﻿using MySql.Data.MySqlClient;
using NB.MGJ.FrameWork;
using NB.MGJ.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Service.Origins
{
    public partial  class OriginsNavService
    {
        private BaseRepository _baseRepository = new BaseRepository();

        #region  增
        public int Add(Origins_Nav model)
        {
            int result = _baseRepository.InsertReturnID<Origins_Nav>(model);
            return result;
        }
        #endregion

        #region 删
        public int Del(int id)
        {
            var sql = string.Format(" UPDATE Origins_Nav  SET ViewDel=1 WHERE NavId = {0}", id);
            int result = _baseRepository.ExecuteSql<int>(sql);
            return result;

        }
        #endregion

        #region 改
        public int Update(Origins_Nav model)
        {
            int result = _baseRepository.Update<Origins_Nav>(model);
            return result;
        }
        #endregion

        #region 查
        public Origins_Nav GetModel(int id)
        {
            string sql = "select * from Origins_Nav where NavId=" + id;
            var model = _baseRepository.ExecuteSqlCollection<Origins_Nav>(sql).ToList().FirstOrDefault();
            return model;
        }



        public PagedList<Origins_Nav> GetPageList(ref PagerModel pager)
        {

            try
            {
                StringBuilder strSql1 = new StringBuilder();
                strSql1.Append(@" Origins_Nav ");
                var strSql = new StringBuilder(strSql1.ToString().Trim(','));
                pager.FldName = " * ";
                pager.TblName = strSql.ToString();

                if (string.IsNullOrEmpty(pager.StrCondition) || pager.StrCondition == " ")
                {
                    pager.StrCondition = " 1=1 ";
                }
                if (string.IsNullOrEmpty(pager.FldSort) || pager.FldSort == "")
                    pager.FldSort = "  ChangeDate desc ";

                MySqlParameter[] parameters = _baseRepository.SqParameterRefactor(ref pager);


                IEnumerable<Origins_Nav> resultList = _baseRepository.ExecuteStoredProcedureCollection<Origins_Nav>("Common_PageList", parameters);
                return new PagedList<Origins_Nav>(resultList.ToList(), pager.Page, pager.PageSize, 1, Convert.ToInt32(parameters[6].Value));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        


        #endregion

        #region  其他
        public List<Origins_Nav> GetNavAllByModuleId(int? id)
        {
            string sql = "select * from Origins_Nav where ModuleId=" + id+" and ViewDel=0";
            var result = _baseRepository.ExecuteSqlCollection<Origins_Nav>(sql).ToList();
            return result;
        }
        #endregion



    }
}
